package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.PyqAct;
import com.qianniu.napi.admin.service.IPyqActService;
import com.qianniu.napi.admin.service.IRedisService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * <p>
 * </p>
 *
 * @author chenxuebin
 * @since 2018-12-17
 */
@Controller
public class PyqActController {

    private static final Logger logger = LoggerFactory.getLogger(PyqActController.class);

    @Autowired
    private IPyqActService pyqactService;
    @Autowired
    private IRedisService redisService;

    @RequestMapping("admin/pyqact/index")
    public String index() {
        return "admin/pyqact";
    }

    @RequestMapping("/admin/pyqact/list")
    @ResponseBody
    public Page<PyqAct> list(@RequestParam(value = "offset", required = false) Integer offset,
                             @RequestParam(value = "status", required = false) Integer status,
                             @RequestParam(value = "search", required = false) String search,
                             @RequestParam(value = "limit", required = false) Integer limit) {
        Wrapper<PyqAct> wrapper = new EntityWrapper<PyqAct>();
        wrapper.eq("mid", ObjectUtil.getMid());
//        if (!Strings.isNullOrEmpty(search))
//            wrapper.like("order_no", search).or().like("remark", search);

        wrapper.orderBy("id", false);
        Page<PyqAct> page = pyqactService.selectPage(new Page<PyqAct>(offset / limit + 1, limit), wrapper);
        return page;
    }

    @RequestMapping("/admin/pyqact/add")
    public String add(@Valid PyqAct bean, BindingResult bindingResult) {
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            return null;
        }
        bean.setCreatetime(new Date());
        bean.setMid(ObjectUtil.getMid());
        bean.setUid(ObjectUtil.getUid());
        bean.insert();
        return "admin/pyqact";
    }

    @RequestMapping("/admin/pyqact/getPyqActById")
    @ResponseBody
    public PyqAct getPyqActById(long id) {
        PyqAct pyqact = pyqactService.selectById(id);
        return pyqact;
    }

    @RequestMapping("/admin/pyqact/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = pyqactService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/pyqact/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        PyqAct bean = new PyqAct();
        bean.setId(id);
        bean.setState(state);
        pyqactService.updateById(bean);
        return "success";
    }

}
